package com.niit.order.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author yaojianfeng
 */
@RestController
@RequestMapping("/hystrix")
public class HystrixTestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("{param}")
    @HystrixCommand(fallbackMethod = "showAppInfoFallback")
    public String showAppInfo(@PathVariable("param") String param) {

        String url = "http://userservice/hystrix/"+param;
        return restTemplate.getForObject(url, String.class);
    }


    @GetMapping("/sleep/{param}")
    @HystrixCommand(fallbackMethod = "showAppInfoFallback")
    public String showAppInfoSleep(@PathVariable("param") String param) {

        String url = "http://userservice/hystrix/sleep/"+param;
        return restTemplate.getForObject(url, String.class);
    }


    @GetMapping("/byParam/{param}")
    @HystrixCommand(fallbackMethod = "showAppInfoFallback")
    public String showAppInfoByParam(@PathVariable("param") String param) {

        String url = "http://userservice/hystrix/byParam/"+param;
        return restTemplate.getForObject(url, String.class);
    }


    /**
     * 回调方法
     * @param param
     * @return
     */
    public String showAppInfoFallback(@PathVariable("param") String param) {
        return "服务器繁忙,请稍后再试o(╥﹏╥)o => " + param;
    }
}
